<template>
	<view class="content">
		<view class="img">
			<open-data type="userAvatarUrl" ></open-data>
		</view>
		<view class="line"/>
		<text  style="color: rgba(1,1,1,0.8)">申请获取以下权限</text><br/>
		<text style="color: rgba(1,1,1,0.4);font-size: 28upx">获得你的公开信息（昵称、头像等）</text>
		<button type="primary" style="width: 90%;margin:40upx 0 0 5%;border-radius: 80upx"
				 bindtap="primary" @click="goUserInfo" open-type="getUserInfo"> 授权登录 </button>
	</view>
</template>

<script>
	import API from '../../common/api.js';
	export default {
		data() {
			return {

			}
		},
		onShow() {
		},
		methods: {
			goUserInfo(){
				uni.showLoading()
				uni.getUserInfo({
					success:(r)=>{
						let userInfo = r.userInfo
						console.log(r)
						wx.login({
							success (res) {
								if (res.code) {
									API.userWaLogin({
										"avatar": userInfo.avatarUrl,
										"gender": userInfo.gender==1?'male':'female',
										"jsCode": res.code,
										"name": userInfo.nickName}).then(res=>{
											console.log(res)
										if(res.status==200){
											API.bankAccount({token: res.token}).then(r=>{
												if(r.status==200&&r.data){
													uni.setStorage({
														key: 'data',
														data: JSON.stringify(r.data),
														success: function () {

														}
													});
												}
											})
											uni.setStorage({
												key: 'token',
												data: res.token,
												success: function () {
													uni.hideLoading()
													uni.showToast({
														title: '登录成功！',
														icon: 'none',
														duration: 2000
													});
												}
											});
											uni.navigateBack();
										}else{
											uni.hideLoading()
											uni.showModal({
												title: '提示',
												content: '登录失败！',
												showCancel: false,
											});
										}
									})
								} else {
									uni.hideLoading()
									uni.showModal({
										title: '提示',
										content: '登录失败！',
										showCancel: false,
									});
								}
							}
						})

					}
				})
			}
		}
	}
</script>

<style scoped>
	.content {
	}
	.img{
		width: 200upx;
		height:200upx;
		border: 2px solid silver;
		border-radius: 100upx;
		margin:15% 0 0 calc(50% - 100upx);
		overflow: hidden;
		box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
	}
	.line{
		width: 90%;
		border-bottom: 1px solid rgba(1,1,1,0.1);
		margin: 40upx 5%;
	}
	text{
		padding-left: 5%;
		line-height: 80upx;
	}
</style>
